using System.Collections.Generic;
using AdvancedInspector;
using I2.Loc;
using UnityEngine;
using UnityEngine.SceneManagement;

[AdvancedInspector(true)]
public class MissionTargetCheckpoint : MissionTargetTimer
{
	public EntityForMT triggerTarget;

	public List<CheckpointForMT> arrCheckpoint = new List<CheckpointForMT>();

	public PointerPanel pointer;

	public GameObject arrow;

	public GameObject arrowBig;

	[Inspect]
	public void AddNewCheckpoint()
	{
		CheckpointForMT[] componentsInChildren = GetComponentsInChildren<CheckpointForMT>(true);
		CheckpointForMT[] array = componentsInChildren;
		foreach (CheckpointForMT item in array)
		{
			if (!arrCheckpoint.Contains(item))
			{
				arrCheckpoint.Add(item);
			}
		}
	}

	[Inspect]
	public void AddArrowOnAllPoint()
	{
		CheckpointForMT[] array = Object.FindObjectsOfType<CheckpointForMT>();
		foreach (CheckpointForMT checkpointForMT in array)
		{
			if (arrow != null)
			{
				if (checkpointForMT.arrow != null)
				{
					Object.DestroyImmediate(checkpointForMT.arrow);
				}
				checkpointForMT.arrow = Object.Instantiate(arrow);
				checkpointForMT.arrow.transform.parent = checkpointForMT.transform;
				checkpointForMT.arrow.transform.localPosition = Vector3.zero;
			}
			if (arrowBig != null)
			{
				if (checkpointForMT.bigArrow != null)
				{
					Object.DestroyImmediate(checkpointForMT.bigArrow);
				}
				checkpointForMT.bigArrow = Object.Instantiate(arrowBig);
				checkpointForMT.bigArrow.transform.parent = checkpointForMT.transform;
				checkpointForMT.bigArrow.transform.localPosition = Vector3.zero;
			}
		}
	}

	[Inspect]
	public void SetNameCheckPoint()
	{
		for (int i = 0; i < arrCheckpoint.Count; i++)
		{
			arrCheckpoint[i].gameObject.name = (i + 1).ToString();
		}
	}

	private void Start()
	{
		if (SceneManager.GetActiveScene().name != "Lobby")
		{
		}
		if (curMission != null && curMission.isCompleted)
		{
			ResetCheckpoint();
		}
	}

	private void OnDisable()
	{
		if (curParametr.isStarted)
		{
			OnStopTarget();
		}
	}

	public override bool ShowTimer()
	{
		return true;
	}

	protected override void OnShowStartMsg()
	{
		MessageManager.instance.CreateMessage(ScriptLocalization.Get("Game_Common/start_mis_checkpoint"), IdClForMsg.levelUp, TypeAnimMessage.longTimeMessage, 5f);
	}

	protected override bool OnStartTarget()
	{
		curCountTarget = 0;
		needCountTarget = arrCheckpoint.Count;
		if (!base.OnStartTarget())
		{
			return false;
		}
		curParametr.maxValue = arrCheckpoint.Count;
		base.transform.position = Vector3.zero;
		ActivateCheckpoint(curCountTarget);
		curParametr.UpdateValue();
		return true;
	}

	public override void OnStopTarget()
	{
		base.OnStopTarget();
		if (IndicatorMissions.instance != null)
		{
			IndicatorMissions.instance.SetActiveTimer(false);
		}
		if ((bool)pointer)
		{
			pointer.Remove();
		}
		ResetCheckpoint();
		ResetTimer();
		if (curCountTarget == needCountTarget && GameController.instance != null && GameController.instance.playerScript != null && GameController.instance.playerScript.currentCar != null && GameController.instance.playerScript.currentCar.Type != CarType.Motorcycle)
		{
			GameController.instance.playerScript.GetOutOfCar(true);
		}
	}

	protected override void OnTimeStart()
	{
		if (IndicatorMissions.instance != null)
		{
			IndicatorMissions.instance.SetActiveTimer(true, (int)timeForComplete);
		}
		MessageManager.instance.CreateMessage(base.messageStart, IdClForMsg.levelUp, TypeAnimMessage.longTimeMessage, 5f);
	}

	protected override void OnTimeOut()
	{
		if (!TargetIsComplete())
		{
			ManagerMissions.OnMissionFaled();
			OnStartTarget();
		}
	}

	protected override void OnUpdateTimer()
	{
		if (IndicatorMissions.instance != null)
		{
			IndicatorMissions.instance.SetTime((int)remainingTime);
		}
	}

	public void OnChangeCheckPoint(CheckpointForMT curCP)
	{
		if (!curParametr.isComplete)
		{
			curCountTarget++;
			if (curCountTarget == 1 && useTimer)
			{
				StartTimer(true);
			}
			ActivateCheckpoint(curCountTarget);
			curParametr.UpdateValue();
			if (TargetIsComplete())
			{
				CompleteTarget();
			}
		}
	}

	private void ResetCheckpoint()
	{
		for (int i = 0; i < arrCheckpoint.Count; i++)
		{
			arrCheckpoint[i].SetStateCheckpoint(CheckpointForMT.stateCP.hide);
			Vector3 priv = Vector3.zero;
			Vector3 target = Vector3.zero;
			if (i != 0)
			{
				priv = arrCheckpoint[i - 1].transform.position;
			}
			if (i != arrCheckpoint.Count - 1)
			{
				target = arrCheckpoint[i + 1].transform.position;
			}
			arrCheckpoint[i].SetArrowTarget(priv, target);
		}
	}

	private void ActivateCheckpoint(int num)
	{
		if (num < 0 || num >= arrCheckpoint.Count)
		{
			return;
		}
		ResetCheckpoint();
		arrCheckpoint[num].SetStateCheckpoint(CheckpointForMT.stateCP.showAndActiv);
		int num2 = 0;
		for (int i = num + 1; i < arrCheckpoint.Count; i++)
		{
			if (num2 >= 2)
			{
				break;
			}
			arrCheckpoint[i].SetStateCheckpoint(CheckpointForMT.stateCP.show);
			num2++;
		}
	}
}
